<!--suppress ALL -->
<template>
	<div class="page maintain-page remainBuild-page" id="remainBuild-page_{{getNewId}}" data-page="allBuild"
		style="z-index: 2">
		<div class="link-page-nav">
			<a href="#" class="link" @click=$root.backToTab()>
				<i class="iconfont icon-zuo"></i>
			</a>
		</div>
		<div class="link-page-top">
			<div class="link-page-title" id="maintainListTitle_{{getNewId}}">
				<span>-</span>
				<div>-</div>
			</div>
			<!-- <a id="btn_allBuild_topRight_{{getNewId}}" class="link popover-open" data-popover="#allBuild_menu_{{getNewId}}"> <i class="iconfont icon-APPicon_gengduo" style="font-size: 24px;"></i>
		</a> -->
			<a href="#" id="allBuild_menu_changeList_{{getNewId}}" @click="changeList()" class="btn-buildDetail">
				<i class="iconfont icon-tuijianbaobiao" style="font-size: 24px"></i>
			</a>
		</div>
		<div class="page-content " id="allBuildList_{{getNewId}}" style="overflow: hidden;">

			<div class="area-change">
				<a href="#" class="popover-close" @click="changeBuild">
					<span id="levelName_{{getNewId}}">-</span>
					<i class="iconfont icon-APPicon_xiala" style="font-size: 14px;"></i>
				</a>
			</div>
			<div class="area-list  allBuild-level-task" id="areaList_{{getNewId}}" style="height:calc(100% - 195px)">
				<div class="area-list-left allBuild-level">
					<div class="area-list-content">
						<div style="height: 5%;"></div>
						<div class="area-list-data" id="floorList_{{getNewId}}"></div>
						<div style="height: 5%;"></div>
					</div>
				</div>
				<div class="area-list-right allBuild-tsak infinite-scroll-content ptr-content pdbottom120"
					data-infinite-distance="50" @infinite="loadMore" @ptr:refresh="refresBuildList"
					style="height: 100%;overflow: auto;padding-bottom: 0;position: relative;">
					<div style="width: 100%;
			height: 30px;
			position: fixed;
			left: 0;
			bottom: 0;
			background: -webkit-linear-gradient(top,rgba(255,255,255,.1) 0% ,rgba(255,255,255,1) 100%);
			pointer-events: none;
		"></div>
					<div class="ptr-preloader">
						<div class="preloader color-multi"></div>
						<div class="ptr-arrow"></div>
					</div>
					<div id="taskList_{{getNewId}}">

					</div>
				</div>
				<div id="buildList_preloader_{{getNewId}}" class="preloader infinite-scroll-preloader"></div>
			</div>
		</div>
		<!-- <div class="popover select-option-con" id="allBuild_menu_{{getNewId}}">
		<div class="popover-angle"></div>
		<div class="popover-inner">
			<div class="list">
				<ul>
					<li id="allBuild_menu_changeList_{{getNewId}}">
						<a href="#" @click="changeList" class="list-button item-link popover-close" id="equiptList_{{getNewId}}">切换为清单视图</a>
					</li>
					<li id="allBuild_menu_addStaffView_{{getNewId}}" style="display:none">
						<a href="#" class="list-button item-link popover-close" id="addStaff_{{getNewId}}">添加责任人</a>
					</li>
				</ul>
			</div>
		</div>
	</div> -->
		<div class="device-scanning" @click="scanning" style="display:none" id="maintainScanner_{{getNewId}}">
			<i class="iconfont icon-weibiaoti--2"></i>
		</div>
	</div>
</template>

<script>
	return {
		mounted() {
			var self = this;
			var app = self.$app;
			self.taskId = this.$route.params.taskId;
			self.orgId = this.$route.params.taskOrgId;
			self.orgName = this.$route.params.orgName;
			self.taskName = decodeURIComponent(this.$route.params.taskName);
			self.jumpFromPage = this.$route.params.pageName;
		},
		data: function () {
			return {
				getNewId: (new Date()).getTime(),
				pageName: "allBuild",
				taskId: 0,
				orgId: 0,
				buildId: 0,
				selectFloorId: 0,
				builds: [],
				buildMame: "",
				orgName: "",
				taskName: "",
				jumpFromPage: "",
				buildSchedule: "", // 楼栋完成进度
				qrCode: "",
				allowInfinite: true,
				eachBuildData: [],
				workAcoountsStatus: 0, //状态：0：表示未开始，1：表示开始，是当前用户已加入2：表示已开始，当前用户未加入
				htmlType: 0, //type维保类型 1 维保巡查任务 2保养任务 3测试任务 4联动测试任务
				hide_BtnAddStaffView_Flag: true
			}
		},
		methods: {
			//任务完成后回到当前页面，刷新当前楼栋
			backToRefresh: function () {
				let self = this;
				// self.getFbsMaintainTaskFinishResult();
				self.getTestTaskStatus()
			},
			//原生扫一扫
			scanning: function () {
				native.scanQrCode();
			},
			//原生扫一扫回调
			scanningreciviceLocation: function (qRcode) {
				var self = this;
				let maintainType = null;

				if (self.jumpFromPage == "maintainInspect" || self.jumpFromPage == "maintainInspectDetail") { //维保巡查
					maintainType = 1;
				} else if (self.jumpFromPage == "maintainDetail" || self.jumpFromPage == "maintain") { //联动测试
					maintainType = 2;
				} else if (self.jumpFromPage == "singleTest" || self.jumpFromPage == "singleTestDetail") { //单点测试
					maintainType = 3;
				}
				Dao.scanVCodeResult({
					taskId: self.taskId,
					qrCode: qRcode,
					maintainType: maintainType //1 维保巡查任务 2保养任务 3测试任务 4联动测试任务
				}, function (data) {
					if (data.status == 200) {
						var id = data.data.businessId;
						var type = data.data.businessType;
						if (self.workAcoountsStatus == 0) {
							confirmDetails(self.taskId, self.getTestTaskStatus, 0, id, type, data.data.id)
							return;
						}
						if (self.workAcoountsStatus == 2) {
							confirmDetails(self.taskId, self.getTestTaskStatus, 2, id, type, data.data.id)
							return;
						}
						if (self.jumpFromPage == "maintainInspect" || self.jumpFromPage ==
							"maintainInspectDetail") {
							app.router.navigate("/maintainInspectTask/" + self.taskId + "/" + id + "/" + type +
								"/");
						} else if (self.jumpFromPage == "maintain") {
							app.router.navigate("/maintainTask/" + self.taskId + "/" + id + "/" + type + "/");
						} else if (self.jumpFromPage == "singleTest" || self.jumpFromPage ==
							"singleTestDetail") {
							app.router.navigate("/singleTestTask/" + self.taskId + "/" + id + "/" + type +
								"/" + data.data.id + "/");
						}
					} else {
						custom.tipSimple(3, data.info);
					}
				});
			},
			changeList: function () {
				var self = this;
				var status = this.$route.params.status;
				if (self.jumpFromPage == "maintainInspect" || self.jumpFromPage == "maintainInspectDetail") {
					app.router.navigate("/maintainInspectDetail/" + self.taskId + "/allBUild/");
				} else if (self.jumpFromPage == "maintainDetail" || self.jumpFromPage == "maintain") {
					app.router.navigate("/maintainDetail/" + self.taskId + "/allBUild/");
				} else if (self.jumpFromPage == "singleTest" || self.jumpFromPage == "singleTestDetail") {
					app.router.navigate("/singleTestDetail/" + self.taskId + "/allBUild/");
				}
			},
			getTestTaskStatus: function (flag,forceRefresh) { //获取当前用户任务状态
				var self = this;
				var $ = self.$$;
				common.loading(1);
				$("#buildList_preloader_" + self.getNewId).hide();
				self.allowInfinite = false;
				if (self.jumpFromPage == "maintainInspect" || self.jumpFromPage == "maintainInspectDetail") {
					self.htmlType = 1;
				} else if (self.jumpFromPage == "maintainDetail" || self.jumpFromPage == "maintain") {
					self.htmlType = 2;
				} else if (self.jumpFromPage == "singleTest" || self.jumpFromPage == "singleTestDetail") {
					self.htmlType = 3;
				} else {
					self.htmlType = 4;
				}
				Dao.getTestTaskStatus({
					userName: userInfor.accountName,
					account: userInfor.accountName,
					taskId: self.taskId, //任务id
					type: self.htmlType, //type维保类型 1 维保巡查任务 2保养任务 3测试任务 4联动测试任务
				}, function (data) {
					self.workAcoountsStatus = data.status;
					//获取楼层数据
					self.getFbsMaintainTaskFinishResult(flag,forceRefresh);
				});
			},
			//获取楼栋数据
			getFbsMaintainTaskFinishResult: function (flag,forceRefresh) {
				var self = this;
				Dao.getFbsMaintainTaskFinishResult({
					orgIds: self.orgId,
					taskId: self.taskId,
					allTaskFlg: 0, // 0-全部  1-剩余
				}, function (data) {
					common.loading(0);
					if (data.length > 0) {
						self.builds = data;
						self.transBuildData(data);
						//展示第一条数据
						if (flag && self.buildId == 0) {
							self.buildId = data[0].fbsId;
						}
						//展示左边楼层
						self.showFloor(flag,forceRefresh);
					} else {
						$("#allBuildList_" + self.getNewId).html(`<div class="no-data-img"><img src="` +
							listNoDataPic() + `"/></div>`);
					}
				}, false);
			},
			//处理楼栋数据
			transBuildData: function (data) {
				var self = this;
				self.eachBuildData = [];
				for (let i = 0; i < data.length; i++) {
					let obj = {};
					obj.fbsName = data[i].fbsName;
					obj.fbsId = data[i].fbsId;
					obj.node = data[i].fbsArr;
					obj.finishCount = data[i].finishCount;
					obj.totalCount = data[i].totalCount;
					self.eachBuildData[data[i].fbsId] = obj;
				}
			},
			//展示左边楼层和头部楼栋
			showFloor: function (flag,forceRefresh) {
				let self = this;
				let currentLevalData = self.eachBuildData[self.buildId];
				//头部楼栋
				let buildSchedule = "0%";
				if (currentLevalData.finishCount != 0 && currentLevalData.totalCount != 0) {
					buildSchedule = ((currentLevalData.finishCount / currentLevalData.totalCount) * 100).toFixed(2) +
						"%";
				}
				$("#levelName_" + self.getNewId).html(currentLevalData.fbsName + " " + buildSchedule);
				//左边楼层
				$("#areaList_" + self.getNewId + " .allBuild-level,#areaList_" + self.getNewId + " .allBuild-tsak")
					.hide();
				jQuery("#areaList_" + self.getNewId + " .no-data-img").remove();
				$("#floorList_" + self.getNewId).empty();
				$("#taskLists_" + self.getNewId).empty();
				let fbsArr = currentLevalData.node;
				if (fbsArr && fbsArr.length > 0) {
					jQuery("#areaList_" + self.getNewId + " .allBuild-level,#areaList_" + self.getNewId +
						" .allBuild-tsak").show();
					for (var i = 0; i < fbsArr.length; i++) {
						if (fbsArr[i].totalCount == fbsArr[i].finishCount) {
							$("#floorList_" + self.getNewId).append(`
							<div class="area-list-item">
								<input type="hidden" value="` + fbsArr[i].id + `">
						          <span><i class="iconfont icon-duigou" style="font-size: 10px;color:#69BA71"></i>` + fbsArr[i].text + `</span>
					        </div>
							`);
						} else {
							$("#floorList_" + self.getNewId).append(`
							<div class="area-list-item">
							<input type="hidden" value="` + fbsArr[i].id + `">
					          <span>` + fbsArr[i].text + `</span>
					        </div>
							`);
						}
					}
					$("#floorList_" + self.getNewId).append(`<div class="" style="height: 100px;"> </div>`)
					//判断self.selectFloorId是否在fbsArr中
					let isExist = false
					jQuery.each(fbsArr, function (index, obj) {
						if (self.selectFloorId == obj.id) {
							isExist = true
							return false
						}
					})
					if (flag && (!isExist||forceRefresh)) {
						self.selectFloorId = fbsArr[0].id;
						self.getAssetsMaintainTaskDetail();
						jQuery("#floorList_" + self.getNewId + " .area-list-item").eq(0).addClass(
							"area-list-item-active").append(`<div class="all-build"></div>`);
					} else {
						var el = jQuery("#floorList_" + self.getNewId + " .area-list-item").find("input");
						if (el.length > 0) {
							for (var i = 0; i < el.length; i++) {
								if (el.eq(i).val() == self.selectFloorId) {
									el.eq(i).parent().addClass("area-list-item-active");
									el.eq(i).parent().append(`<div class="all-build"></div>`);
								}
							}
						}
					}
					self.getTaskByFloor();
				}
			},
			// 根据楼层获取任务信息
			getAssetsMaintainTaskDetail: function () {
				var self = this;
				$("#taskList_" + self.getNewId).empty();
				if (!self.selectFloorId) {
					app.methods.showToastCenter("该楼栋下暂未配置楼层，无法查看");
					return
				}
				Dao.getAssetsMaintainTaskDetail({
					fbsId: self.selectFloorId,
					taskId: self.taskId,
					allTaskFlg: 0 // 0-全部 1-剩余
				}, function (data) {
					var maintainAccount = "";
					if (data.length > 0) {
						for (var i = 0; i < data.length; i++) {
							var maintainAccount = data[i].maintainAccount;
							if (data[i].finishFlg == 0) {
								if (userInfor.role[0].id == 183 || userInfor.role[0].id == 184) {
									self.hide_BtnAddStaffView_Flag = false
								} else {
									self.hide_BtnAddStaffView_Flag = true
								}
								self.hideBtn_changeList();

								if (maintainAccount && maintainAccount.indexOf(userInfor.accountName) >=
									0) { //当前登录人是责任人
									jQuery("#maintainScanner_" + self.getNewId).show();
								} else {
									jQuery("#maintainScanner_" + self.getNewId).hide();
								}
							}
							if (data[i].businessType == 2) { // 区域
								if (data[i].finishFlg == 0) { //未完成
									$("#taskList_" + self.getNewId).append(`
											<div class="device-list-item tasking" data-type="2" data-id="` + data[i].fbsId + `" data-detailId="` + data[
										i].taskDetailId + `">
									        <div class="device-name">` + data[i].fbsName + `</div>
											<span class="device-type">含维保设备</span>
											<span class="device-num">` + data[i].assetsCount + `个</span>
										</div>
									`);
								} else {
									$("#taskList_" + self.getNewId).append(`
										<div class="device-list-item device-complete">
										<div class="device-name">` + data[i].fbsName + `</div>
										<div class="device-type area-complate">
											含维保设备
											<label class="device-num">` + data[i].assetsCount + `个</label>
											<span class="complete-icon">
											  <i class="iconfont icon-duigou" style="font-size: 24px;"></i></span>
										</div>
									</div>
									`);
								}
							} else {
								if (data[i].finishFlg == 0) { //未完成
									$("#taskList_" + self.getNewId).append(`
											<div class="device-list-item tasking" data-type="1" data-id="` + data[i].assetsId + `" data-detailId="` +
										data[i].taskDetailId + `">
											    <div class="device-name">` + data[i].fbsName + `</div>
									            <div class="device-type">
													<span class="single-type">单</span>` + data[i].assetsName + `
												</div>
											</div>
										`);
								} else {
									$("#taskList_" + self.getNewId).append(`
											<div class="device-list-item device-complete">
											   <div class="device-name">` + data[i].fbsName + `</div>
												<div class="device-type">
													<span class="single-type">单</span>` + data[i].assetsName + `
									 				<span class="complete-icon">
											          <i class="iconfont icon-duigou" style="font-size: 24px;"></i>
										    	    </span>
												</div>
											</div>
										`);
								}
							}
						}
					} else {
						$("#taskList_" + self.getNewId).append(`<div class="no-data-img"><img src="` +
							listNoDataPic() + `"/></div>`);
					}
					// 开始任务
					jQuery("#taskList_" + self.getNewId + " .tasking").off().click(function () {
						if (maintainAccount && maintainAccount.indexOf(userInfor.accountName) >=
							0) { //当前登录人是责任人
							var type = this.getAttribute("data-type");
							var id = this.getAttribute("data-id");
							var detailId = this.getAttribute("data-detailId");
							if (self.workAcoountsStatus == 0) {
								confirmDetails(self.taskId, self.getTestTaskStatus, 0, id, type,
									detailId)
								return;
							}
							if (self.workAcoountsStatus == 2) {
								confirmDetails(self.taskId, self.getTestTaskStatus, 2, id, type,
									detailId)
								return;
							}
							//保养
							if (self.jumpFromPage == "maintainDetail" || self.jumpFromPage ==
								"maintain") {

								if (self.workAcoountsStatus == 2) {
									app.methods.showToastCenter('请先加入任务');
									return;
								}
								app.router.navigate("/maintainTask/" + self.taskId + "/" + id + "/" +
									type + "/");
							}
							//单点测试详情
							else if (self.jumpFromPage == "singleTest" || self.jumpFromPage ==
								"singleTestDetail") {
								if (self.workAcoountsStatus == 2) {
									app.methods.showToastCenter('请先加入任务');
									return;
								}
								app.router.navigate("/singleTestTask/" + self.taskId + "/" + id + "/" +
									type + "/" + detailId + "/");
							}
							//维保巡查列表/维保巡查详情
							else if (self.jumpFromPage == "maintainInspect" || self.jumpFromPage ==
								"maintainInspectDetail") {
								app.router.navigate("/maintainInspectTask/" + self.taskId + "/" + id +
									"/" + type + "/");
							}
						} else {
							app.methods.showToastCenter('非当前责任人，不可开始任务');
						}
					});
				}, true);
			},
			// 切换楼栋
			changeBuild: function () {
				var self = this;
				self.viewBuild(function (data) {
					self.buildId = data.fId;
					self.showFloor();
				});
			},
			// 查看楼栋
			viewBuild: function (callback, fId) {
				var self = this;
				var data = self.builds;
				var str = "";
				for (var i = 0; i < data.length; i++) {
					var schedule = "0%";
					if (data[i].finishCount != 0 && data[i].totalCount != 0) {
						schedule = ((data[i].finishCount / data[i].totalCount) * 100).toFixed(2) + "%";
					}
					str += `<div class="rdsp-option-list-` + self.getNewId +
						` rdsp-option-list rdsp-option-list-border all-level" data-index="` + i + `" data-id="` + data[
							i].fbsId + `" data-name="` + data[i].fbsName + `" data-text="` + schedule + `">
								<span>` + data[i].fbsName + `</span>
								<span style="float:right;">完成度  ` + schedule + `</span>
							</div>`;
				}
				openLayer(str, (el) => {
					jQuery(".rdsp-option-list-" + self.getNewId + ".all-level").off().click(function (event) {
						var index = jQuery(event.currentTarget).attr("data-index");
						var id = jQuery(event.currentTarget).attr("data-id");
						var name = jQuery(event.currentTarget).attr("data-name");
						var schedule = jQuery(event.currentTarget).attr("data-text");
						if (callback) {
							callback({
								"index": index,
								"fId": id,
								"fName": name,
								"fSche": schedule
							});
						}
						$("#taskList_" + self.getNewId).empty()
						jQuery("#floorList_" + self.getNewId + " .area-list-item").removeClass(
							"area-list-item-active")
						jQuery("#floorList_" + self.getNewId + " .area-list-item .all-build")
							.removeClass()
						jQuery("#floorList_" + self.getNewId + " .area-list-item").siblings(
							"area-list-item-active")
						jQuery("#floorList_" + self.getNewId + " .area-list-item").eq(0).addClass(
							"area-list-item-active").append(`<div class="all-build"></div>`);
						layer.closeAll();
						self.selectFloorId = jQuery("#floorList_" + self.getNewId).find(
							'.area-list-item-active').find('input').val()
						self.getAssetsMaintainTaskDetail();
					});
				});
			},
			//点楼层显示任务
			getTaskByFloor: function () {
				var self = this;
				jQuery("#floorList_" + self.getNewId + " .area-list-item").off().click(function () {
					if (getClickEnabel()) {
						$("#taskList_" + self.getNewId).empty();
						self.selectFloorId = this.firstElementChild.value;
						$("#floorList_" + self.getNewId + " .area-list-item.area-list-item-active")
							.removeClass("area-list-item-active");
						$("#floorList_" + self.getNewId + " .area-list-item .all-build").remove();
						$(this).addClass("area-list-item-active");
						$(this).append(`<div class="all-build"></div>`);
						self.getAssetsMaintainTaskDetail();
					}
				});
			},
			htmlPage: function (forceRefresh) {
				var self = this;
				var $ = self.$$;
				$("#buildList_preloader_" + self.getNewId).hide();
				self.allowInfinite = false;

				$("#maintainListTitle_" + self.getNewId + " span").html(self.orgName);
				$("#maintainListTitle_" + self.getNewId + " div").html(self.taskName);
				self.getTestTaskStatus(true,forceRefresh);

			},
			loadMore: function () {
				var self = this;
				if (!self.allowInfinite) return;
				self.allowInfinite = false;
				setTimeout(function () {
					self.allowInfinite = true;
				}, 1000);
			},
			refresBuildList: function (e, done) {
				if (done && e) {
					var self = this;
					var $ = self.$$;
					var w = document.getElementById("taskList_" + self.getNewId);
					// if(w.scrollTop==0){
					setTimeout(function () {
						self.reInit();
						$("#buildList_preloader_" + self.getNewId).hide();
						self.getTestTaskStatus(0);
						done();
					}, 1000);
					// }else{
					// 	done();
					// 	$("#buildList_preloader_" + self.getNewId).hide();
					//}
				}
			},
			reInit: function () {
				var self = this;
				if (app.view.current.router.url.indexOf('/linkTest/') > -1) {
					//防止子页面返回触发。有类似的需求可以往这个里面添加
					$("#remainBuild-page_" + self.getNewId + " .page-content").scrollTop(0, 300);
				}
				//$("#linkTest_dataList").empty();
				if (!self.allowInfinite) {
					self.allowInfinite = true;
					$("#buildList_preloader_" + self.getNewId).show();
				}
				self.hideBtn_changeList();
			},
			//是否隐藏切换为清单视图
			hideBtn_changeList() {
				var self = this;
				jQuery("#btn_allBuild_topRight_" + self.getNewId).show();
				/* if (self.hide_BtnAddStaffView_Flag) {
					jQuery("#allBuild_menu_addStaffView_" + self.getNewId).hide();
				}else{
					jQuery("#allBuild_menu_addStaffView_" + self.getNewId).show();
				} */
				//保养  单点测试详情  维保巡查详情
				if (self.jumpFromPage == "maintainDetail" || self.jumpFromPage == "singleTestDetail" || self
					.jumpFromPage == "maintainInspectDetail") {
					jQuery("#allBuild_menu_changeList_" + self.getNewId).hide();
					if (self.hide_BtnAddStaffView_Flag) {
						jQuery("#btn_allBuild_topRight_" + self.getNewId).hide();
					}
				} else {
					jQuery("#allBuild_menu_changeList_" + self.getNewId).show();
				}
			}
		},
		on: {
			pageInit: function (e, page) {
				var self = this;
				var status = this.$route.params.status;
				$("#addStaff_" + self.getNewId).attr("href", "/maintainOwner/" + self.orgId + "/" + self.taskId +
					"/allBuild/''/");
				self.reInit();
				self.htmlPage();
			},
		}
	}
</script>
<style scoped>
	.all-build {
		width: 10px;
		height: 10px;
		background-color: #2471CA;
		margin-left: 47px;
		transform: rotate(40deg);
		position: relative;
		top: -25px;
	}
</style>